<?php
	require_once('../includes/config.php');
	$action = "add";
	$statusbtn = "Add new";
	$readonly = "";
	$catname = "";
	$catdes  = "";
	if($_REQUEST['act'] == "edit")
	{
		$catid = $_GET['id'];
		$statusbtn = "Update";
		$readonly = "readonly";
		$str_query = "SELECT * FROM tbl_category WHERE id = '$catid'";
		$resul = mysql_query("$str_query") or die ("can not load data for update ");
		$data = mysql_fetch_assoc($resul);
		$catname = $data['name'];
		$catdes = $data['description'];
		$action = "edit";
		
	}
?>
<script type="text/javascript">
$(document).ready(function() {
	var name = $('#cat_name');
	var nameInfo = $("#catNameInfo");
	name.blur(valCatName);
	name.keyup(valCatName);
	function valCatName(){
		if(name.val().length < 4) {
			nameInfo.text("least 4 characters");
			nameInfo.removeClass("isok");
			nameInfo.addClass("error");
			return false;
		}
		else {
			nameInfo.text("Its look good");
			nameInfo.removeClass('error');
			nameInfo.addClass('isok');
			return true;
		}
	}
	$("form#catForm").submit(function(){
		if(valCatName())
		{
		var id = $('#cat_id').attr('value');
		var des = $('#cat_des').attr('value')
		var act = $("#act").attr('value');
		var datastring = 'act=' + act +
						 '&id=' + id +
						 '&name=' + name.attr('value') +
						 '&des=' + des;
		 $.ajax({ 
			 url: "category_process.php",
			 type: "POST",
			 data: datastring,
			 dataType: 'json',
			 success: function(data){
				 if(data==1)
				 {
					$("#rounded-corner").load("category_list.php");
					disablePopup();
					$("form").each( function() { this.reset; });
				 }
				 else
				 {
					 alert('Some error occur!');
				 }
			 }
		 });
		 return false;
		}
		else
			return false;
	});
});
</script>
 
		<a id="popupCatClose">x</a>
		<h1>Add/edit category</h1>
        <form id="catForm" method="post">
        <p>
        <input type="hidden" name="cat_id" id="cat_id" value="<?php echo $catid; ?>" />
          <label>Category Name:</label>
          <input type="text" name="cat_name" id="cat_name" value="<?php echo $catname; ?>" />
           <span id="catNameInfo">This field is require!</span>
     </p>
     <p>
          <label>Category Description:</label>
          <input type="text" name="cat_des" id="cat_des" value="<?php echo $catdes; ?>" />
        </p>
        <input id="act" type="submit" name="act" value="<?php echo $action; ?>" />
        </form>
		
	
